package com.Mt.Mager.Contorller;

import com.Mt.Dto.Dto;
import com.Mt.Service.MtOrderService;
import com.Mt.Utils.DtoUtil;
import com.Mt.pojo.Mt_Order;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 用户订单管理
 */
@RestController
@RequestMapping("Order")
public class Mt_OrderController {

    @Reference
    private MtOrderService mtOrderService;

    /**
     * 查询所有订单数据
     */
    @GetMapping("list")
    private Dto order() {
        List<Mt_Order> mtOrder = mtOrderService.selById();

        if (mtOrder == null) {
            return DtoUtil.returnError(5003, "没有该用户");
        } else {
            return DtoUtil.returnSuccessData(mtOrder);
        }
    }

    /**
     * 删除一条订单数据
     */
    @GetMapping("DelOrder/{ordernumber}")
    private Dto DelOrder(@PathVariable("ordernumber") int ordernumber) {
        int result = mtOrderService.DelOrder(ordernumber);
        if (result > 0) {
            return DtoUtil.returnSuccess();
        } else {
            return DtoUtil.returnError(5001, "未知错误");
        }
    }


    /**
     * 添加一条新的订单数据
     */
    @PostMapping("AddOrder")
    private Dto AddOrder(@RequestBody Mt_Order mtOrder) {
        Mt_Order order = new Mt_Order();
        final double d = Math.random();
        final int i = (int) (d * 10000000);
        //订单编号
        order.setOrderNumber(i);
        //用户ID
        order.setOrderUserid(mtOrder.getOrderUserid());
        //订单状态
        order.setOrderState(201);
        //支付金额
        order.setOrderMoney(mtOrder.getOrderMoney());
        //执行方法
        int result = mtOrderService.AddOrder(order);

        if (result > 0) {
            return DtoUtil.returnSuccess();
        }else {
            return DtoUtil.returnError(5001,"未知错误");
        }
    }
    /**
     * 查询对应ID的订单
     */
    @GetMapping("SelById/{id}")
    public Dto SelById(@PathVariable("id") String id){
        List<Mt_Order> mtOrder = mtOrderService.selByID(id);
        if (mtOrder == null){
            return DtoUtil.returnError(5001,"查询失败");
        }else {
            return DtoUtil.returnSuccessData(mtOrder);
        }

    }

}
